/**
 * Copyright © 2014 TingerSure. All rights reserved.
 */
package org.sure.scomet.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sure.scomet.link.ConnectionCenter;
import org.sure.scomet.util.log.LogManager;
import org.sure.scomet.util.log.LogType;

/**
 * The action which tell the server to close the connection.
 * 
 * @author TingerSure
 */
public class CloseAction {

	/**
	 * The close action.<br>
	 * It will be called when the close request come.
	 * 
	 * @param req
	 *            the request
	 * @param res
	 *            the response
	 */
	public void Action(HttpServletRequest req, HttpServletResponse res) {
		int id = Integer.parseInt(req.getParameter("id"));
		if (!ConnectionCenter.getCallBack().role(id, req.getSession())) {
			LogManager.log(
					"wrong request role in close -> " + req.getRemoteUser()
							+ "@" + req.getRemoteHost() + ":"
							+ req.getRemotePort() + ":", LogType.warn);
			return;
		}
		ConnectionCenter.closeConnection(id);
		PrintWriter write = null;
		try {
			write = res.getWriter();
			write.write("c:");
		} catch (IOException e) {
			LogManager.logException("message return error ", e, LogType.error);
		}
	}

}
